Data Types

Objectives


Discussion

Variables and Data Types

Legal Variable Names

Using Variables

Dim sName as String
Dim iCount as Integer
Dim bFlag as Boolean

sName = "Mary"
iCount = -100
bFlag = False

Defining and Using Constants

Const NumberMonths = 12
Const NumberMonths As Short = 12

Back to top


Demonstration

1.  Create a new project.  We suggest that you call it "demos unit 2" or similar and use this project for all of the demonstrations in this unit. 

2.  Add a button: Name = "btnAssign" and Text = "Assign".  Add a textboxName = "txtData" and Text = "".  Add a label: Name = "lblValue" and Text = "". A label is a simple version of a textbox except that a user can not enter information into it. Generally we use labels to display information or to add a caption for another control.

3.  In the click event for btnAssign, declare a variable called iData and make it an Int16 or Short.  Then assign the value in txtData.text to iData.  Then assign the value in iData to lblValue.text.  Your event should look like:

Private Sub btnAssign_Click...
Dim iData As Int16
iData = txtData.Text
lblValue.text = iData
End Sub

4.  Notice in the above code that we are using very inefficient code to demonstrate how we can assign values to variables and then use variables.  If you really wanted to assign the value of the textbox to a label you could use one line of code and would not need to use the iData variable:  lblValue.text = txtData.text

5. Run the program. Type "100" into the textbox and click the button. Then type "1000" into the textbox and click the button. Continue increasing the size of the value in the textbox until an error occurs.  You should eventually get an overflow error.  Notice that VS highlights the line of code that causes the error. You get this error because an Int16 (or Short) type can hold values up to 32767.  If you try to put a value greater than 32767 you are overflowing the variable.  Try typing 32767 and then 32768. 

6.  Now change the iData type to "byte" by changing Int16 to byte .   Repeat step  #5.  What values now give an error?  What if you type a negative number in the textbox?

7.  Now type a letter or bunch of letters into the textbox and click the button.  You should get an error.  You get an error because you typed a string into the textbox but the variable iData is expecting a number.  Notice the error message.  You should become familiar with the error messages so that when you see them in the future you will know what they mean.

8.  Comment out the line Dim iData As Byte by putting a single quote (') in front of it.  Notice that iData is now underlined.  Hold the mouse over the underlined iData.  Notice that you get a message that iData is not declared.  This message means that you never declared what iData is. 

9.  Now let's redeclare iData as something else to continue experimenting with data types.Type in the following after the line that you commented out:

Dim iData As String

10. Run the program and type a bunch of things in the textbox. Try numbers and words. Notice that you do not get an error. That is because a string can be numbers and/or letters.

11.  Now comment out the line iData = txtData.text and add the following highlighted line so that your code looks like:

' Dim iData As Byte
Dim iData As String
iData = "bob"
'iData = txtData.Text
lblValue.Text = iData

12. Run your program. Now change your code by removing the quotes around the word "bob" so that the line looks like: iData = bob.Run the program. You should get an error because the program wants to treat the word "bob" as a variable instead of a string. Whenever you assign a value to a string you must put quotes around it. The same is true with a character type.

13.  Now we will experiment with the Boolean type.  Add the following code somewhere in your button event:

Dim bData As Boolean
bdata =

Notice that after you type the "=" VS provides a list of possible options.  Since you declared bData to be Boolean, it can only have a value of True or False.  Click one of them.

14.  Now we will experiment with initializing and declaring variables.  Comment out all of the code in the button click event and type the following code.  This code declares and initializes two variables and then assigns the value of sName1 to sName2. A very important concept is buried in this code. Notice that when a variable is on the left side of the equal sign, it is given the value of the variable on the right side. The value on the right side does NOT change and does NOT get the value of the left.

Dim sName1 As String = "Pat"
Dim sName2 As String = "Mary"
sName2 = sName1
lblValue.Text = sName2

15. Run the code. Notice that sName2 now has the value "Pat." Now change the last line so that sName1 is displayed in the label. Run the program again. Notice that sName1 is still "Pat".

Back to top

Exercises

1.  Look up Single and Double in Help. What is the difference?  Create a simple program like you did in the Demo to show the maximum values that they can hold. 

2.  For each of the following situations described declare a variable using the most appropriate type. In some situations multiple types are valid.

a.  Stores a text message.
b. Stores the area of a circle.
c. Keeps track of whether or not something has happened.
d. Stores a social security number.
e. Stores the length of your desk in miles.
f. Stores the number of video games for a game system.
g. Stores the temperature in degrees fahrenheit.
h. Stores someone's name.
i. Stores the slope of a line.
j. Stores the number of stars in the universe.

3. Add a constant to a program for the number of periods in a school day and display the constant in a label.

4.  Declare and initialize a string named sName to "YourName".   Display the string on the face of a button when the button is clicked.

5.  In each group identify the word that is not a valid variable name?

a.  fName, ?Name, i, xyz
b.  iNum, INUM, _nuM, 6Num
c.  myNum, blah, C6, my num
d.  Dim, iDim, Dim4, dimMe

6.  Explain the terms: "Declare", "Initialize", and "Assign".  Write an example of each.

Back to top
Links & Help
Back to top